package com.pms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pms.common.PageBean;
import com.pms.mapper.BizAskLeaveMapper;
import com.pms.pojo.BizAskLeave;
import com.pms.service.BizAskLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BizAskLeaveServiceImpl implements BizAskLeaveService {

    @Autowired
    private BizAskLeaveMapper bizAskLeaveMapper;

    @Override
    public PageBean<BizAskLeave> getLeaveList(String name, String leaveType, Integer status,
                                              String beginTime, String endTime, Integer page, Integer pageSize) {

        // 设置分页参数
        PageHelper.startPage(page, pageSize);

        // 执行查询
        List<BizAskLeave> leaveList = bizAskLeaveMapper.getLeaveList(name, leaveType, status, beginTime, endTime);
        Page<BizAskLeave> pageResult = (Page<BizAskLeave>) leaveList;

        // 封装结果
        PageBean<BizAskLeave> pageBean = new PageBean<>();
        pageBean.setTotal(pageResult.getTotal());
        pageBean.setRows(pageResult.getResult());

        return pageBean;
    }

    @Override
    @Transactional
    public void approve(Long id, String opinion, Long auditorId) {
        // 查询请假记录
        BizAskLeave leave = bizAskLeaveMapper.selectById(id);
        if (leave == null) {
            throw new RuntimeException("请假记录不存在");
        }
        if (leave.getStatus() != 0) {
            throw new RuntimeException("该请假记录已处理");
        }

        // 更新状态
        leave.setStatus(1);
        leave.setAuditId(auditorId);
        leave.setAuditOpinion(opinion);
        leave.setAuditTime(new Date());

        int rows = bizAskLeaveMapper.updateById(leave);
        if (rows != 1) {
            throw new RuntimeException("审批失败");
        }
    }

    @Override
    @Transactional
    public void reject(Long id, String opinion, Long auditorId) {
        // 查询请假记录
        BizAskLeave leave = bizAskLeaveMapper.selectById(id);
        if (leave == null) {
            throw new RuntimeException("请假记录不存在");
        }
        if (leave.getStatus() != 0) {
            throw new RuntimeException("该请假记录已处理");
        }

        // 更新状态
        leave.setStatus(2);
        leave.setAuditId(auditorId);
        leave.setAuditOpinion(opinion);
        leave.setAuditTime(new Date());

        int rows = bizAskLeaveMapper.updateById(leave);
        if (rows != 1) {
            throw new RuntimeException("驳回失败");
        }
    }


    @Override
    @Transactional
    public void submitLeave(BizAskLeave leave) {
        // 基础验证
        if (leave.getStartTime() == null || leave.getEndTime() == null) {
            throw new RuntimeException("请假时间不能为空");
        }
        if (leave.getStartTime().after(leave.getEndTime())) {
            throw new RuntimeException("开始时间不能晚于结束时间");
        }
        if (leave.getLeaveHour() == null || leave.getLeaveHour() <= 0) {
            throw new RuntimeException("请假时长必须大于0");
        }
        if (leave.getLeaveReason() == null || leave.getLeaveReason().trim().isEmpty()) {
            throw new RuntimeException("请假事由不能为空");
        }

        // 插入记录
        int rows = bizAskLeaveMapper.insert(leave);
        if (rows != 1) {
            throw new RuntimeException("提交请假申请失败");
        }
    }

    @Override
    public List<BizAskLeave> getLeavesByUserId(Long userId) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        return bizAskLeaveMapper.selectByUserId(userId);
    }

    @Override
    @Transactional
    public void cancelLeave(Long id, Long userId) {
        // 查询请假记录
        BizAskLeave leave = bizAskLeaveMapper.selectById(id);
        if (leave == null) {
            throw new RuntimeException("请假记录不存在");
        }

        // 验证是否是本人的请假记录
        if (!leave.getUserId().equals(userId)) {
            throw new RuntimeException("无权操作他人的请假记录");
        }

        // 验证状态是否为待审核
        if (leave.getStatus() != 0) {
            throw new RuntimeException("只能撤销待审核的请假申请");
        }

        // 更新状态为已撤销
        leave.setStatus(3);
        int rows = bizAskLeaveMapper.updateById(leave);
        if (rows != 1) {
            throw new RuntimeException("撤销失败");
        }
    }

    @Override
    public BizAskLeave getLeaveById(Long id) {
        if (id == null) {
            throw new RuntimeException("请假记录ID不能为空");
        }
        BizAskLeave leave = bizAskLeaveMapper.selectById(id);
        if (leave == null) {
            throw new RuntimeException("请假记录不存在");
        }
        return leave;
    }

}